
import Vue from 'vue'



//判断用户是否需要登录/注册
function checkLoginState () {
	uni.checkSession({//登录过期
		fail: () => {
			console.info('sessionKey过期')
		}
	})
	if(!uni.getStorageSync('userId') || 
	!uni.getStorageSync('phone') || 
	!uni.getStorageSync('xcxtoken') 
	){
		return false;
	}else{
		return true;
	}
				
}
Vue.prototype.checkLoginState = function(){
	return checkLoginState();
}

var _width = uni.getSystemInfoSync().windowWidth;
var _height = uni.getSystemInfoSync().screenHeight;
Vue.prototype.miniProgramWH = _height;	

//rpx转px，以350px为宽度的屏幕，1rpx = 0.5px，即 350px -> 750rpx ，rpx换算px的比例则为宽度/750
const getrpxTopx = (rpx) => _width / 750 * rpx;
Vue.prototype.getrpxTopx = getrpxTopx;	

//获取页面剩余高度 - 用在有地图页面
Vue.prototype.getRemainHeight = function(rpx){
	let rpxTopx = getrpxTopx(rpx);
	return _height - rpxTopx;
}

// 保留当前页，跳转到某页
Vue.prototype.toPage = function (url,loginPage,notNeedLogin = true,mark) { 	
	console.info('-----loginstate-----',url,checkLoginState())

	if(!url && !mark){
		uni.navigateTo({
			url:'/pages/index/empty'
		})
		return;
	}
	if(notNeedLogin){
		uni.navigateTo({
			url: url,
			animationType: 'slide-in-bottom',
			animationDuration: 500
		})
	}else{
		if(checkLoginState()){
			uni.navigateTo({
				url: url,
				animationType: 'slide-in-bottom',
				animationDuration: 500
			})
		}else{//未登录/注册
			uni.navigateTo({
				url:loginPage
			})	
		}
	}
	
}

//消息订阅
Vue.prototype.setTempMsg = function(type, callback) {
  let ids = [];
  switch (type) {
	//order、pay为汽车售票的消息订阅，暂时不使用
    case 'order':
      // 购票成功订阅消息ID
      ids.push('JicMRtj8yGpGiz69TqUdNsK63uHmowI8MswJOzwxHv0');
      // 购票失败订阅消息ID
      ids.push('f8PxHMYRUGIqKtEpp79HF4-N4tzRtzCiCD8m93dyYWE');
      // 发车提醒通知ID
      ids.push('HoTnW4yaiPR8586gPZh49SNduAs5bXBqhno3nGrpRJQ');
      break;
    case 'pay':
      // 退票成功订阅消息ID
      ids.push('vVdg5UA4_7ph6-Kh6K1rqLM7GFQu6eG7CnMmbAeY50A');
      // 改签成功订阅消息ID
      ids.push('WKHz8vagn0JfOgO_02Utt0jSC0ScvSa5qU5dcFUYbW4');
      break;
    case 'charter':
      // 下单成功通知ID
      ids.push('odcZf1iIWI2CWu9eYNOnA01tAsTpbWgqNHw6507uxCo');
      // 订单取消成功通知ID
      ids.push('2v_3_j74KoTzWGVlbgvfKA1JAlwerZIAGFfLPY26QWQ');
      // 派车成功通知ID
	  // 4wHEhmNlD5s7w_-yE3dPTezqMtTHHXf9i4UKNwq4HC4
      ids.push('4wHEhmNlD5s7w_-yE3dPTQheDYJxkgeZWaCFsCjjob4');
	  // // 出车提醒订阅消息ID
	  // ids.push('egU-quXA1Tc7mauJ3QEU7UyDegHysziM7DFfioyheQw');
	  // // 司机已到达通知ID
	  // ids.push('Y6yE3mUZeH6ncg6Bmkyf4uP5Z4HNULBLjiOqACarexM');
	  // // 行程结束通知ID
	  // ids.push('IRjk31E5xppwe-vEn0ioItnwxzA594DAC_L1v6A2HIw');
      break;
    case 'charter_pay':
      // 出车提醒订阅消息ID
      ids.push('egU-quXA1Tc7mauJ3QEU7UyDegHysziM7DFfioyheQw');
      // 司机已到达通知ID
      ids.push('Y6yE3mUZeH6ncg6Bmkyf4uP5Z4HNULBLjiOqACarexM');
      // 行程结束通知ID
      ids.push('IRjk31E5xppwe-vEn0ioItnwxzA594DAC_L1v6A2HIw');
      break;
    default:
      break;
  }
  uni.requestSubscribeMessage({
    tmplIds: ids,
    fail(res) {
        console.log('===发送订阅消息===',res);
		callback('fail');
    },
    complete(res) {
		console.log('===发送订阅消息===',res);
        callback();
    },
  });
}

